package com.xuecheng.base.model;

import lombok.Data;
import lombok.ToString;

/**
 * 统一返回结果类
 * @param <T>
 */
@Data
@ToString
public class RestResponse<T> {
    /**
     * 0为正常，-1为错误
     */
    private int code;

    private String msg;

    private T result;

    public RestResponse(int code,String msg){
        this.code = code;
        this.msg = msg;
    }

    public RestResponse(){
        this(0,"success");
    }

    public static<T> RestResponse<T> success(){
        return new RestResponse<T>();
    }

    public static<T> RestResponse<T> success(T result){
        RestResponse<T> r = new RestResponse<>();
        r.setResult(result);
        return r;
    }

    public static<T> RestResponse<T> success(T result,String msg){
        RestResponse<T> r = new RestResponse<>(0,msg);
        r.setResult(result);
        return r;
    }

    public static <T> RestResponse<T> validfail(String msg) {
        RestResponse<T> response = new RestResponse<T>();
        response.setCode(-1);
        response.setMsg(msg);
        return response;
    }
    public static <T> RestResponse<T> validfail(T result,String msg) {
        RestResponse<T> response = new RestResponse<T>();
        response.setCode(-1);
        response.setResult(result);
        response.setMsg(msg);
        return response;
    }

    public Boolean isSuccessful(){
        return this.code == 0;
    }
}
